﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net; 
using System.Text;
using System.Web;
using Newtonsoft.Json;

namespace BrightcoveAPI
{
    // <summary>
    // Wrapper class for making Brightcove API Requests
    // </summary>    
    public class BCAPIRequest
    {
        #region Private Properties

        private HttpWebRequest webRequest;

        #endregion Private Properties

        public JsonReader Execute( Dictionary<String,String> reqParams )
        {
            String reqUrl = BCAPI.Configuration.ServiceURL + "?";
            int i = 0;
            foreach (String key in reqParams.Keys)
            {
                if (i > 0) reqUrl += "&";
                reqUrl += String.Format("{0}={1}", key, HttpUtility.UrlEncode(reqParams[key]));
                i++;
            }
            webRequest = WebRequest.Create(reqUrl) as HttpWebRequest;
            HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
            
            TextReader textreader = new StreamReader(response.GetResponseStream());
            
            //Uncomment to see the actual JSON string in the console
            //Console.WriteLine(textreader.ReadToEnd());

            JsonReader reader = new JsonTextReader(textreader);
            return reader;
        }

    }
}
